package com.online.usermanagesystem.service.impl;

import com.online.usermanagesystem.dao.AccountMapper;
import com.online.usermanagesystem.entity.Account;
import com.online.usermanagesystem.service.AccountManageService;
import com.online.usermanagesystem.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 账号管理的处理类
 *
 * @create 2018-04-24 10:35
 */
@Component
public class AccountManageServiceImpl implements AccountManageService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Object query(Integer id) {
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        Account account;
        try {
            account = accountMapper.query(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (null == account) {
            return ResultUtil.NOT_FOUND;
        }

        return account;
    }

    @Override
    public Object add(Account account) {

        if ((null == account) || (null == account.getName()) || (null == account.getPassword())) {
            return ResultUtil.BAD_REQUEST;
        }

        if (null == account.getNickname()) {
            account.setNickname(account.getName());
        }

        if(null == account.getAvatar()) {
            account.setAvatar("");
        }

        String formatPassword = getMD5(account.getPassword());
        if (null == formatPassword) {
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        Integer count;
        try {
            count = accountMapper.countByName(account.getName());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (0 != count) {
            return ResultUtil.CONFLICT;
        }
        account.setPassword(formatPassword);

        try {
            accountMapper.insert(account);
        } catch (DuplicateKeyException e) {
            return ResultUtil.CONFLICT;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.returnKV("id", account.getId());
    }

    @Override
    public Object modify(Account account) {
        if (null == account) {
            return ResultUtil.BAD_REQUEST;
        }

        Integer id = account.getId();
        if (null == id) {
            return ResultUtil.BAD_REQUEST;
        }

        //不允许修改的字段
        account.setName(null);
        account.setPassword(null);

        String password = account.getPassword();
        if (null != password) {
            account.setPassword(getMD5(password));
        }

        Integer count;
        try {
            count = accountMapper.countById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (0 == count) {
            return ResultUtil.NOT_FOUND;
        }

        try {
            accountMapper.update(account);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return ResultUtil.OK;
    }

    @Override
    public Object auth(String name, String password) {
        String formatPassword = getMD5(password);
        if (null == formatPassword) {
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        Account account;
        try {
            account = accountMapper.auth(name, formatPassword);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        if (null == account) {
            return ResultUtil.UNAUTHORIZED;
        }

        return account;
    }

    @Override
    public Object list(String keyword, Integer index, Integer count) {
        if (null == index) {
            index = 0;
        }

        if (null == count) {
            count = 64;
        }

        List<Account> list;
        try {
            if (null == keyword) {
                list = accountMapper.querys(index, count);
            } else {
                list = accountMapper.search(keyword.concat("%"), index, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.INTERNAL_SERVER_ERROR;
        }

        return list;
    }

    private String getMD5(String value) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(value.getBytes("UTF-8"));
            byte[] md5Bytes = messageDigest.digest();
            char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
            char[] resultCharArray = new char[md5Bytes.length * 2];
            int index = 0;
            for (byte b : md5Bytes) {
                resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
                resultCharArray[index++] = hexDigits[b & 0xf];
            }

            return new String(resultCharArray);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
